from pymongo import MongoClient
from pprint import pprint

import codecs
import csv

client = MongoClient('47.104.130.19', 27017)

qianchengwuyou_db = client['qianchengwuyou_db']
dajiewang_db = client['dajiewang_db']

qianchengwuyou_company = qianchengwuyou_db.qianchengwuyou_company
dajiewang_company = dajiewang_db.dajiewang_company

info = {

}


def pack(file_name):
    csv_file = codecs.open(file_name, 'w', 'utf_8_sig')

    writer = csv.DictWriter(csv_file, fieldnames = ['name', 'type', 'site', 'industry', 'from', 'keep', 'industry_search', 'location', 'size', 'introduce', 'logo'])

    companys = [comp for coll in [qianchengwuyou_company, dajiewang_company] for comp in list(coll.find({
        "keep": 'yes',
        'industry_search': '法律'
    }))]

    for comp in companys:
        if comp['name'][:7] in info:
            continue

        del comp['_id']

        writer.writerow(comp)
        writer.writerow({
            'name': '',
            'type': '',
            'site': '',
            'industry': '',
            'from': '',
            'keep': '',
            'industry_search': '',
            'location': '',
            'size': '',
            'introduce': '',
            'logo': '',
        })

        info[comp['name'][:7]] = True

    csv_file.close()


pack('company.csv')
# pprint(companys)
